package cn.wolfcode.business.service.impl;

import java.util.Date;
import java.util.List;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.domain.BusStatementItem;
import cn.wolfcode.business.mapper.BusAppointmentMapper;
import cn.wolfcode.business.mapper.BusStatementItemMapper;
import cn.wolfcode.business.util.RegexUtils;
import cn.wolfcode.business.util.VehiclePlateNoUtil;
import cn.wolfcode.common.exception.ServiceException;
import cn.wolfcode.common.utils.DateUtils;
import cn.wolfcode.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.BusStatementMapper;
import cn.wolfcode.business.domain.BusStatement;
import cn.wolfcode.business.service.IBusStatementService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 结算单Service业务层处理
 * 
 * @author dalang
 * @date 2023-10-31
 */
@Service
@Transactional
public class BusStatementServiceImpl implements IBusStatementService 
{
    @Autowired
    private BusStatementMapper busStatementMapper;
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;
    @Autowired
    private BusStatementItemMapper busStatementItemMapper;

    /**
     * 查询结算单
     * 
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public BusStatement selectBusStatementById(Long id)
    {
        return busStatementMapper.selectBusStatementById(id);
    }

    /**
     * 查询结算单列表
     * 
     * @param busStatement 结算单
     * @return 结算单
     */
    @Override
    public List<BusStatement> selectBusStatementList(BusStatement busStatement)
    {
        return busStatementMapper.selectBusStatementList(busStatement);
    }

    /**
     * 新增结算单
     * 
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    public int insertBusStatement(BusStatement busStatement)
    {
        if (busStatement==null) {
            throw new ServiceException("非法参数");
        }
        if (!RegexUtils.isPhoneLegal(busStatement.getCustomerPhone())) {
            throw new ServiceException("非法手机号");
        }
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busStatement.getLicensePlate());
        if (vehiclePlateNo==null) {
            throw new ServiceException("非法车牌号");
        }
        if (busStatement.getActualArrivalTime().before(new Date())) {
            throw new ServiceException("到店时间不能早于当前时间");
        }

        BusStatement temp = new BusStatement();
        BeanUtils.copyProperties(busStatement,temp);
        temp.setCreateTime(DateUtils.getNowDate());
        temp.setStatus(BusStatement.STATUS_CONSUME);

        return busStatementMapper.insertBusStatement(temp);
    }

    /**
     * 修改结算单
     * 
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    public int updateBusStatement(BusStatement busStatement)
    {
        if (busStatement==null) {
            throw new ServiceException("非法参数");
        }
        if (!BusStatement.STATUS_CONSUME.equals(busStatement.getStatus())) {
            throw new ServiceException("状态为消费中的服务单才能修改");
        }

        if (!RegexUtils.isPhoneLegal(busStatement.getCustomerPhone())) {
            throw new ServiceException("非法手机号");
        }
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busStatement.getLicensePlate());
        if (vehiclePlateNo==null) {
            throw new ServiceException("非法车牌号");
        }
        if (busStatement.getActualArrivalTime().before(new Date())) {
            throw new ServiceException("到店时间不能早于当前时间");
        }
        BusStatement temp = new BusStatement();
        BeanUtils.copyProperties(busStatement,temp);

        return busStatementMapper.updateBusStatement(temp);
    }

    /**
     * 批量删除结算单
     * 
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementByIds(Long[] ids)
    {
        for (Long id : ids) {
            BusStatement busStatement = busStatementMapper.selectBusStatementById(id);
            if (busStatement==null|| BusStatement.STATUS_PAID.equals(busStatement.getStatus())) {
                throw new ServiceException("已支付的订单无法删除");
            }

            busStatement.setIsDelete(BusStatement.IS_DELETE_YES);
            busStatementMapper.updateBusStatement(busStatement);

            busStatementItemMapper.deleteByStatementId(id);

            if (busStatement.getAppointmentId()!=null) {
                busAppointmentMapper.updateStatus(busStatement.getAppointmentId(),BusAppointment.STATUS_ARRIVAL);
            }
        }

        return 1;
    }

    /**
     * 删除结算单信息
     * 
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementById(Long id)
    {
        return busStatementMapper.deleteBusStatementById(id);
    }

    /**
     * 支付结算单
     *
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public void pay(Long id) {
        if (id==null) {
            throw new ServiceException("非法参数");
        }
        BusStatement busStatement = busStatementMapper.selectBusStatementById(id);
        if (busStatement==null || !BusStatement.STATUS_CONSUME.equals(busStatement.getStatus())) {
            throw new ServiceException("状态为消费中的订单才能支付");
        }
        List<BusStatementItem> items = busStatementItemMapper.selectBusStatementItemBySid(id);
        BusStatementItem busStatementItem = new BusStatementItem();
        busStatementItem.setStatementId(id);
        List<BusStatementItem> items2 = busStatementItemMapper.selectBusStatementItemList(busStatementItem);
        System.out.println(items);
        System.out.println(items2);
        if (items.size()<1) {
            throw new ServiceException("没有需要支付的项或尚未保存");
        }

        busStatement.setStatus(BusStatement.STATUS_PAID);
        busStatement.setPayTime(new Date());
        busStatement.setPayeeId(SecurityUtils.getUserId());

        busStatementMapper.updateBusStatement(busStatement);

        //@Todo 后续如果为预约单的结算--修改预约单状态为已支付
        if (busStatement.getAppointmentId()!=null) {
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(busStatement.getAppointmentId());
            if (busAppointment!=null){
                busAppointmentMapper.updateStatus(busStatement.getAppointmentId(),BusAppointment.STATUS_PAYED);
            }
        }
    }
}
